const feedbackModel = require('../model/feedbackModel');
const helpModel = require('../model/helpModel');
const messageCfgModel = require('../model/messageCfgModel');
const smsModel = require('../model/smsModel');
const pushModel = require('../model/pushModel');
const messageModel = require('../model/messageModel');
const util = require('../util/util');

var MessageController = {

	registerAll : function(app,auth){
		app.post('/message/saveFeedback', this.saveFeedback);
		app.get('/message/getFeedbackList', this.getFeedbackList);

		app.post('/message/saveHelp', this.saveHelp);
		app.get('/message/getHelpList', this.getHelpList);

		app.post('/message/config/save', this.saveMessageCfg);
		app.get('/message/config/messageTriggers', this.getMessageTriggers);
		app.get('/message/config/messageTypes',this.getMessageTypes);
		app.get('/message/config/list', this.getMessageCfgList);

		app.post('/message/sms/send',this.sendSms);

		app.post('/message/push/test',this.testPush);
		app.post('/message/push/pushContent',this.push);
		app.post('/message/push/bindTag',this.bindTag);
		app.post('/message/push/unbindTag',this.unbindTag);
		app.get('/message/push/listTags',this.listTags);
		app.get('/message/push/queryDeviceStat',this.queryDeviceStat);

		app.post('/message/sendMessage',this.sendMessage);
		app.get('/message/getMessageList',this.getMessageList);
		app.post('/message/readMessage',this.readMessage);
		app.post('/message/deleteMessage',this.delMessage);
		app.get('/message/getUnreadCount',this.getUnreadCount);

		app.post('/message/sendAction',this.sendAction)
		
	},

	sendAction:function(req, res){
		messageModel.sendAction(req, function(err, ret){
			if(!err){
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	sendMessage: function(req, res){
		messageModel.send(req, function(err, ret){
			if(!err){
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	getMessageList: function(req, res){
		messageModel.getMessageList(req, function(err, ret){
			if(!err){
				res.json( {code:0,data:ret});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	readMessage: function(req, res){
		messageModel.read(req, function(err, ret){
			if(!err){
				res.json( {code : 0,data:ret});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	getUnreadCount: function(req,res){
		messageModel.getUnreadCount(req,function(err,ret){
			if(!err){
				res.json( {code : 0,data:ret});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},

	delMessage: function(req, res){
		messageModel.del(req, function(err, ret){
			if(!err){
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	testPush:function(req, res){
		pushModel.testPush(req, function(err, ret){
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	push: function(req, res){
		pushModel.push(req, (err, ret)=>{
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},

	listTags: function(req, res){
		pushModel.listTags(req, (err, ret)=>{
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},

	bindTag: function(req, res){
		pushModel.bindTag(req, (err, ret)=>{
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},

	unbindTag: function(req, res){
		pushModel.unbindTag(req, (err, ret)=>{
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},

	queryDeviceStat: function(req, res){
		pushModel.queryDeviceStat(req, (err, ret)=>{
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		})
	},


	sendSms:function(req, res){
		smsModel.sendSms(req, function(err, ret){
			if(!err){
				console.log(ret);
				res.json( {code : 0});
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	saveMessageCfg:function(req, res){
		messageCfgModel.save(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},
	getMessageCfgList:function(req, res){
		messageCfgModel.getMessageCfgList(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	getMessageTypes: function(req,res){
		res.json({code:0,data:messageCfgModel.getMessageTypes()})
	},

	getMessageTriggers: function(req, res){
		res.json({code:0,data:messageCfgModel.getTriggers()})
	},

	saveFeedback : function(req , res){
		feedbackModel.save(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	getFeedbackList : function(req , res){
		feedbackModel.getFeedbackList(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	saveHelp : function(req , res){
		helpModel.save(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

	getHelpList : function(req , res){
		helpModel.getHelpList(req, function(err, ret){
			if(!err){
				res.json( {code : 0 , data: ret });
			}
			else{
				res.status(400).json(util.errorFormat(err));
			}
		});
	},

};

module.exports = MessageController;